<!-- 会员 - 退卡 - 弹窗组件 -->
<template>
  <div>
    <CustomDialog v-model:visible="isShowDialog" top="2vh" curent-class="card-refund-dialog">
      <div class="dialog-content">
        <div class="title">退卡</div>
        <div class="dialog-input-box">
          <div class="form-item">
            <div class="desc">
              <div>退款方式</div>
            </div>
            <div class="dialog-input select-input">
              <el-select
                class="select"
                clearable
                v-model="form.refund_type"
                placeholder="请选择退款方式"
              >
                <el-option :value="10" label="现金"></el-option>
                <el-option :value="20" label="微信"></el-option>
                <el-option :value="30" label="支付宝"></el-option>
              </el-select>
            </div>
          </div>
          <div class="form-item">
            <div class="desc">
              <div>退款金额</div>
            </div>
            <div class="dialog-input">
              <input
                clearable
                class="input"
                v-model.trim="form.money"
                type="text"
                placeholder="请输入退款金额"
              />
              <img
                class="image"
                :src="clearImage"
                alt=""
                @click="handleClearInput"
                v-if="form.money"
              />
            </div>
          </div>
        </div>
      </div>
      <!-- 数字键盘组件 -->
      <NumberKeyboard
        v-model:keyword="form.money"
        @onInputKeyboard="onInputKeyboard"
        @onCancel="handleClearInput"
        @onConfirm="onConfirm"
        :loading="keyboardLoading"
      />
      <div class="dinerDialog-bottom-desc" v-if="memberData?.balance">
        会员余额：{{ memberData.balance }}元
      </div>
    </CustomDialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, reactive, watch } from 'vue'
import CustomDialog from '@/components/CustomDialog/Index.vue'
import NumberKeyboard from '@/components/NumberKeyboard/Index.vue'
import { formTestFun } from '@/utils/toolsValidate'

interface Props {
  visible: boolean
  memberData?: any
}

interface Emits {
  (e: 'update:visible', value: boolean): void
  (e: 'onConfirm', formData: any): void
}

const props = withDefaults(defineProps<Props>(), {
  visible: false,
  memberData: () => ({})
})

const emit = defineEmits<Emits>()

// 控制弹窗显示
const isShowDialog = computed({
  get: () => props.visible,
  set: val => emit('update:visible', val)
})

// 数字键盘loading状态
const keyboardLoading = ref(false)

// 表单数据
const form = reactive({
  refund_type: '', // 退款方式
  money: '' // 退款金额
})

// 图片资源
const clearImage = new URL('@/assets/images/home/input-clear.png', import.meta.url).href

// 监听弹窗显示状态
watch(
  () => props.visible,
  newVal => {
    if (newVal) {
      // 重置表单数据
      form.refund_type = ''
      form.money = ''
    }
  }
)

// 确认提交事件
const onConfirm = () => {
  // testForm对象的value值只判断是否为true即可
  const testForm = {
    请输入退款方式: form.refund_type,
    请输入退款金额: form.money
  }
  if (formTestFun(testForm)) return

  keyboardLoading.value = true
  // 模拟异步操作
  setTimeout(() => {
    emit('onConfirm', form)
    emit('update:visible', false)
    keyboardLoading.value = false
  }, 300)
}

// 接收数字键盘传过来的关键词
const onInputKeyboard = (val: string) => {
  form.money = val
}

// 清空输入框
const handleClearInput = () => {
  form.money = ''
}
</script>

<style lang="scss" scoped>
.dialog-content {
  .title {
    font-size: 20px;
    font-family:
      PingFang SC,
      PingFang SC-Bold;
    font-weight: 700;
    text-align: center;
    color: #333333;
  }

  .good-name {
    font-size: 18px;
    font-family:
      PingFang SC,
      PingFang SC-Regular;
    font-weight: 400;
    text-align: center;
    color: #353535;
    margin-top: 30px;
  }

  .dialog-input-box {
    margin-bottom: 60px;
    padding: 0 6px;
    padding-top: 10px;

    .form-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 20px;

      .desc {
        font-size: 16px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        color: #333333;
        margin-right: 10px;
        display: flex;
        align-content: flex-start;
      }

      .dialog-input {
        width: 420px;
        height: 48px;
        background: #ffffff;
        border: 1px solid #d2d2d2;
        border-radius: 12px;
        box-sizing: border-box;
        padding: 0 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .select {
          width: 400px;
          font-size: 14px;
          font-family:
            PingFang SC,
            PingFang SC-Regular;
          font-weight: 400;
          text-align: left;
          color: #333333;
          outline: none;
          border: none !important;
          box-shadow: none !important;
        }

        :deep(.el-select__wrapper) {
          border: none !important;
          box-shadow: none !important;
        }

        .select {
          border: none !important;
          appearance: none;
          -moz-appearance: none;
          -webkit-appearance: none;
        }

        .select::-ms-expand {
          display: none;
        }

        .input {
          width: 400px;
          font-size: 14px;
          font-family:
            PingFang SC,
            PingFang SC-Regular;
          font-weight: 400;
          text-align: left;
          color: #333333;
          outline: none;
          border: none;
        }

        .image {
          flex-shrink: 0;
          width: 24px;
          height: 24px;
          cursor: pointer;
        }
      }
    }
  }
}
.select-input {
  padding: 0 !important;
}
.dinerDialog-bottom-desc {
  font-size: 16px;
  font-family:
    PingFang SC,
    PingFang SC-Regular;
  font-weight: 400;
  text-align: left;
  color: #353535;
  margin-top: 20px;
}
</style>

<style lang="scss">
.card-refund-dialog {
  max-height: unset !important;
}
</style>
